package com.event.config;

import com.event.pojo.User;
import com.event.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    @Override  //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override  //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken)token;
        String username = (String)token.getPrincipal();
        User user = new User();
        user.setEduAccount(Long.valueOf(username));
        List<User> users = userService.selectUser(user);
        if (users.size()==0){
            return null;
        }
        return new SimpleAuthenticationInfo(username,users.get(0).getEduPassword(),"");
    }
}
